<%@page import="java.sql.ResultSet"%>
<%@ page import="java.util.Date" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="true" %>
<%@ page contentType="text/html; charset=UTF-8" import="com.mysql.jdbc.*"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Connection"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.DriverManager"%>
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.Statement"%>

<%!
	public int join(String _strEmail, String _strPwd, String _strMe2dayId, StringBuffer _strbufMessage){
	
		Connection conn = null;
	    Statement stmt = null;
	 	int ret = 0;
	 	
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        ResultSet result = stmt.executeQuery("select * from user where userId='" + _strEmail + "';");
	        if(result.next() == true){
		        stmt.close();
		        conn.close();
		        
		        _strbufMessage.append("이미 존재하는 아이디입니다.");
		        return -1;
	        }
			
	        
	        stmt.execute("insert into user(userId,attName,value) values('" + _strEmail + "', 'password', '" + _strPwd + "');");
	        
	        if(_strMe2dayId != null){
	        	stmt.execute("insert into user(userId,attName,value) values('" + _strEmail + "', 'me2dayId', '" + _strMe2dayId + "');");
	        }else{
	        	stmt.execute("insert into user(userId,attName,value) values('" + _strEmail + "', 'me2dayId', '');");
	        }
	        
	        
	        stmt.close();
	        conn.close();
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	    	ret = -1;
	    }
	
		return ret; 
		
	}

/**
* ID와 pwd에 매칭되는 사용자가 있는지 여부를 반환한다.
*
* @param _strEmail 이메일 주소
* @param _strPassword 패스워드
*
* @retuen 0:성공,0제외 값:매칭되는 사용자 없음 혹은 에러발생
**/
	public int login(String _strEmail, String _strPassword){
		Connection con = null;
	    Statement stmt = null;
	 	int ret = 0;
	 	
	    try
	    {
	    	Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mimo" ,"root" ,"d0o9o84");
	        
	        stmt = conn.createStatement();
	        
	        ResultSet result = stmt.executeQuery("select * from user where userId='" + _strEmail + "' AND attName='password'AND value='" + _strPassword + "';");
	        
	        if(result.next()){
	        	ret = 0;
	        }else{
	        	ret = -1;
	        }
	        
	        result.close();
	        stmt.close();
	        conn.close();
	    }
	    catch( Exception ex )
	    {
	    	ex.printStackTrace();
	    	ret = -1;
	    }
	
		return ret; 
	}

	public void request_login(){
		
		
	}
	
	
	public void request_join(){
		
		
	}
%>
<% 
	session = request.getSession(true);

	String strCmd = request.getParameter("cmd");
	
	int bLoginPage = 0;
	
	if((strCmd != null) && (strCmd.equals("login") == true)){
		String strId = request.getParameter("email");
		String strPwd = request.getParameter("pwd");
		
		int ret = login(strId, strPwd);
		
		response.setContentType("text/xml");
		
		if(ret == 0){
			session.setAttribute("id",strId);
			
			out.println("<result>ok</result>");
			return;
		}else{
			out.println("<result>fail</result>");
			return;
		}
		
	}else if((strCmd != null) && (strCmd.equals("join") == true)){
		String strEmail = request.getParameter("email");
		String strPwd = request.getParameter("pwd");
		String strMe2dayId = request.getParameter("me2day");
		StringBuffer strbufMessage = new StringBuffer();
		
		int ret = join(strEmail, strPwd, strMe2dayId, strbufMessage);
		
		response.setContentType("text/xml");
		
		if(ret == 0){
			session.setAttribute("id",strEmail);
			
			out.println("<result>ok</result>");
			
			return;
		}else{
			bLoginPage = 1;
			
			out.println("<result>fail<error>" + strbufMessage + "</error></result>");
		}
		
	}
	
%>